package org.prs.persistence;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.prs.model.NextOfKin;
import org.prs.persistence.connector.MySQLDatabaseConnector;

public class NextOfKinDatabasePersistence {

    private Connection conn;
    private ResultSet result;

    public NextOfKinDatabasePersistence() {
    }

    public boolean insertNextOfKin(NextOfKin nextOfKin) {
        boolean inserted = true;
        conn = MySQLDatabaseConnector.getConnection();
        PreparedStatement insertNextOfKin = null;
        try {
            insertNextOfKin = conn.prepareStatement("insert into nextofkin set"
                    + " childid=?, relationshiptypeid=?, firstname=?, middlename=?,"
                    + " lastname=?, phonenumber=?");
            insertNextOfKin.setInt(1, nextOfKin.getChildId());
            insertNextOfKin.setInt(2, nextOfKin.getRelationshipTypeId());
            insertNextOfKin.setString(3, nextOfKin.getFirstName());
            insertNextOfKin.setString(4, nextOfKin.getMiddleName());
            insertNextOfKin.setString(5, nextOfKin.getLastName());
            insertNextOfKin.setString(6, nextOfKin.getPhoneNumber());
            insertNextOfKin.executeUpdate();
        } catch (SQLException e) {
            inserted = false;
        }
        return inserted;
    }

    public boolean updateNextOfKin(NextOfKin nextOfKin) {
        boolean updated = true;
        conn = MySQLDatabaseConnector.getConnection();
        PreparedStatement updateNextOfKin = null;
        try {
            updateNextOfKin = conn.prepareStatement("update nextofkin set"
                    + " childid=?, relationshiptypeid=?, firstname=?, middlename=?,"
                    + " lastname=?, phonenumber=? where nextofkinid=?");
            updateNextOfKin.setInt(1, nextOfKin.getChildId());
            updateNextOfKin.setInt(2, nextOfKin.getRelationshipTypeId());
            updateNextOfKin.setString(3, nextOfKin.getFirstName());
            updateNextOfKin.setString(4, nextOfKin.getMiddleName());
            updateNextOfKin.setString(5, nextOfKin.getLastName());
            updateNextOfKin.setString(6, nextOfKin.getPhoneNumber());
            updateNextOfKin.setInt(7, nextOfKin.getNextOfKinId());
            updateNextOfKin.executeUpdate();
        } catch (SQLException e) {
            updated = false;
        }
        return updated;
    }

    public boolean deleteNextOfKin(int nextOfKinId) {
        boolean deleted = true;
        conn = MySQLDatabaseConnector.getConnection();
        PreparedStatement deleteNextOfKin = null;
        try {
            deleteNextOfKin = conn.prepareStatement("delete from nextofkin where nextofkinid=?");
            deleteNextOfKin.setInt(1, nextOfKinId);
            deleteNextOfKin.executeUpdate();
        } catch (SQLException e) {
            deleted = false;
        }
        return deleted;
    }

    public List<NextOfKin> getChildNextOfKins(int childId) {
        List<NextOfKin> nextOfKins = new ArrayList<NextOfKin>();
        conn = MySQLDatabaseConnector.getConnection();
        PreparedStatement selectChildNextOfKins = null;
        try {
            selectChildNextOfKins = conn.prepareStatement("select * from nextofkin where childid=?");
            selectChildNextOfKins.setInt(1, childId);
            result = selectChildNextOfKins.executeQuery();
            while (result.next()) {
                nextOfKins.add(getNextOfKin(result));
            }
        } catch (SQLException e) {
            e.printStackTrace(System.out);
        }
        return nextOfKins;
    }

    public NextOfKin getNextOfKinById(int nextOfKinId) {
        NextOfKin nextOfKin = new NextOfKin();
        conn = MySQLDatabaseConnector.getConnection();
        PreparedStatement selectNextOfKinById = null;
        try {
            selectNextOfKinById = conn.prepareStatement("select * from nextofkin where nextofkinid=?");
            selectNextOfKinById.setInt(1, nextOfKinId);
            result = selectNextOfKinById.executeQuery();
            while (result.next()) {
                nextOfKin = getNextOfKin(result);


            }
        } catch (SQLException e) {
            e.printStackTrace(System.out);
        }
        return nextOfKin;
    }

    public NextOfKin getNextOfKin(ResultSet result) {
        NextOfKin nextOfKin = new NextOfKin();
        try {
            nextOfKin = new NextOfKin();
            nextOfKin.setChildId(result.getInt("childid"));
            nextOfKin.setNextOfKinId(result.getInt("nextofkinid"));
            nextOfKin.setRelationshipTypeId(result.getInt("relationshiptypeid"));
            nextOfKin.setFirstName(result.getString("firstname"));
            nextOfKin.setLastName(result.getString("lastname"));
            nextOfKin.setMiddleName(result.getString("middlename"));
            nextOfKin.setPhoneNumber(result.getString("phonenumber"));
        } catch (SQLException e) {
            e.printStackTrace(System.out);
        }
        return nextOfKin;
    }
}
